#include <cmath>
#include <QDebug>
#include <QFontMetrics>
#include "element.h"

DrawPolygon::DrawPolygon(void)
{
    m_points.clear();
    m_drawType = POLYLINE;
    m_state = DS_IDLE;
}
void DrawPolygon::computeBox(void)
{
    m_box.setCoords ( 0, 0, 0, 0 );
}

void DrawPolygon::setStartPoint(QPoint point)
{
    m_points.clear();
    m_points.push_back(point);
    m_tempEnd = point;
    m_state = DS_STATE1;
    qDebug() << "setStartPoint";
}

void DrawPolygon::setTempEndPoint(QPoint point)
{
    m_tempEnd = point;
    //compute();
}

void DrawPolygon::setEndPoint(QPoint point)
{
    m_points.push_back(point);
    compute();
    updateText();
    //computeBox();
    m_state = Element::DS_FINISH;
}

void DrawPolygon::build(QVector<QPoint> points)
{
    m_points = points;
    compute();
    updateText();
    m_state = Element::DS_FINISH;
}

void DrawPolygon::setMidPoint(QPoint point)
{
    if(m_points.last() != point)
    {
        m_points.push_back(point);
        //m_tempEnd = point;
    }
}

int DrawPolygon::mouseRButtonPress(QPoint point)
{
    if(m_state == DS_STATE1)
    {
        setEndPoint(point);
        return 1;
    }
    return 0;
}

int DrawPolygon::mouseLButtonPress(QPoint point)
{
    if( m_state == DS_IDLE )
    {
        setStartPoint(point);
        return 1;
    }
    if(m_state == DS_STATE1)
    {
        setMidPoint(point);
        return 2;
    }
    return 0;
}

int DrawPolygon::mouseMove(QPoint point)
{
    if( m_state == DS_STATE1 )
    {
        setTempEndPoint(point);
        return 1;
    }
    return 0;
}

void DrawPolygon::updateText(void)
{
    //m_text = QString(QStringLiteral("S = %1 um2")).arg(m_area * m_scale, 2, 'f', 2);
    if(m_area * m_scale * m_scale >= 1e6)
    {
        m_text = QString::fromUtf8(u8"S = %1 mm²").arg(m_area * m_scale * m_scale / 1e6, 2, 'f', 2);
    }
    else
    {
        m_text = QString::fromUtf8(u8"S = %1 um²").arg(m_area * m_scale * m_scale, 2, 'f', 2);
    }
    QFontMetrics fm(m_textFont);
    int textWidth = fm.width(m_text);
    int textHeight = fm.height();
    m_textBox.setWidth(textWidth);
    m_textBox.setHeight(textHeight);

    QPoint start = m_points.first();
    QPoint end = m_points.last();
    int X = (start.x() + end.x()) / 2;
    int Y = (start.y() + end.y()) / 2;
    m_textBox.moveTo(X, Y);
}

void DrawPolygon::compute(void)
{
    int N = m_points.size();
    if(N < 2)
    {
        return;
    }
    m_area = 0;
    for( int i = 0; i < N-1; i++ )
    {
        qDebug() << "p1 = (" << m_points[i].x() <<", " << m_points[i].y() <<"), P2 = (" << m_points[i+1].x() << ", " << m_points[i+1].y() << ")";
        m_area += (m_points[i].x() * m_points[i+1].y() - m_points[i].y() * m_points[i+1].x())/2.0;

    }
    m_area += (m_points[N - 1].x() * m_points[0].y() - m_points[N-1].y() * m_points[0].x())/2.0;
    m_area = fabs(m_area);
}



void DrawPolygon::draw( QPainter &painter )
{
    if( m_state != DS_IDLE )
    {
        painter.save();
        painter.setPen(m_linePen);
        int N = m_points.size() - 1;
        for(int i = 0; i < N; i ++)
        {
            painter.drawLine(m_points[i], m_points[i+1]);
        }
        painter.drawLines(m_points);
        if(m_state == Element::DS_STATE1)
        {
            painter.drawLine(m_points.last(), m_tempEnd);
            painter.drawLine(m_tempEnd, m_points.first());
        }
        if(m_state == Element::DS_FINISH)
        {
            painter.drawLine(m_points.last(), m_points.first());
            painter.setFont(m_textFont);
            painter.drawText(m_textBox, m_text);
        }
        painter.restore();
    }
}


